/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projetluc;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author Lone_Wolf
 */
public class Fenetre extends JFrame {

    public ArrayList<Client> arClient;
    public ArrayList<Employe> arEmploye;
    public ArrayList<Entree> arEntree;
    public ArrayList<Facture> arFactures;
    private String versionBD;
    private JPanel pnlVisible;
    private BarreMenu barreMenu;
    private EcranConnexion pnlEcranConnexion;
    private EcranRecherche pnlEcranRecherche;
    private CreerNouvEntree pnlNouvEntree;
    private CreerNouvClient pnlNouvClient;
    private CreerNouvEmploye pnlNouvEmploye;
    private ConsulterClient pnlConsulterClient;
    private ModifierEntree pnlModifierEntree;
    private ModifierClient pnlModifierClient;
    private ModifierEmploye pnlModifierEmploye;
    private ConsulterFacture pnlConsulterFacture;
    private ModifierFacture pnlModifierFacture;
    private StatsClient pnlStatsClient;
    private StatsCompletes pnlStatsCompletes;
    private Employe utilCourant;

    public Fenetre() throws IOException, ClassNotFoundException {
        super("Nom du Programme");

        pnlVisible = new JPanel(new BorderLayout());
        setExtendedState(Frame.MAXIMIZED_BOTH);
        this.setLayout(new BorderLayout());
        this.add(pnlVisible, BorderLayout.NORTH);
        versionBD = "02";

        chargerBD();
        AfficherEcranConnexion();
    }

    public void AfficherEcranConnexion() throws IOException, ClassNotFoundException {
        this.setMenuBar(null);
        pnlVisible.removeAll();
        pnlEcranConnexion = new EcranConnexion(this);
        setExtendedState(Frame.MAXIMIZED_BOTH);
        pnlVisible.add(pnlEcranConnexion, BorderLayout.NORTH);
        pnlVisible.revalidate();
        pnlVisible.repaint();
    }

    public void connectionValide(Employe utilCourant) throws IOException, ClassNotFoundException {
        this.utilCourant = utilCourant;
        AfficherEcranRecherche();
        AfficherBarreMenu();
    }

    public void AfficherEcranRecherche() throws IOException, ClassNotFoundException {
        pnlVisible.removeAll();
        pnlEcranRecherche = new EcranRecherche(this);
        setExtendedState(Frame.MAXIMIZED_BOTH);
        pnlVisible.add(pnlEcranRecherche);
        pnlVisible.revalidate();
        pnlVisible.repaint();
    }

    public void AfficherEcranNouvClient() throws IOException, ClassNotFoundException {
        pnlVisible.removeAll();
        pnlNouvClient = new CreerNouvClient(this);
        setExtendedState(Frame.MAXIMIZED_BOTH);
        pnlVisible.add(pnlNouvClient);
        pnlVisible.revalidate();
        pnlVisible.repaint();
    }

    public void AfficherEcranNouvEmploye() throws IOException, ClassNotFoundException {
        pnlVisible.removeAll();
        pnlNouvEmploye = new CreerNouvEmploye(this);
        setExtendedState(Frame.MAXIMIZED_BOTH);
        pnlVisible.add(pnlNouvEmploye);
        pnlVisible.revalidate();
        pnlVisible.repaint();
    }

    public void AfficherEcranNouvEntree() throws IOException, ClassNotFoundException {
        pnlVisible.removeAll();
        pnlNouvEntree = new CreerNouvEntree(this);
        setExtendedState(Frame.MAXIMIZED_BOTH);
        pnlVisible.add(pnlNouvEntree);
        pnlVisible.revalidate();
        pnlVisible.repaint();
    }

    private void AfficherBarreMenu() {
        barreMenu = new BarreMenu(this);
        this.setMenuBar(barreMenu);
    }

    public void afficherConsulterClient(Client client) {
        pnlVisible.removeAll();
        pnlConsulterClient = new ConsulterClient(this, client);
        setExtendedState(Frame.MAXIMIZED_BOTH);
        pnlVisible.add(pnlConsulterClient);
        pnlVisible.revalidate();
        pnlVisible.repaint();
    }

    public void afficherModifierEntree(Entree entree) {
        pnlVisible.removeAll();
        pnlModifierEntree = new ModifierEntree(this, entree);
        setExtendedState(Frame.MAXIMIZED_BOTH);
        pnlVisible.add(pnlModifierEntree);
        pnlVisible.revalidate();
        pnlVisible.repaint();
    }

    public void afficherModifierEmploye(SearchResult result) {
        pnlVisible.removeAll();
        pnlModifierEmploye = new ModifierEmploye(this, result);
        setExtendedState(Frame.MAXIMIZED_BOTH);
        pnlVisible.add(pnlModifierEmploye);
        pnlVisible.revalidate();
        pnlVisible.repaint();
    }

    public void afficherModifierClient(Client client) {
        pnlVisible.removeAll();
        pnlModifierClient = new ModifierClient(this, client);
        setExtendedState(Frame.MAXIMIZED_BOTH);
        pnlVisible.add(pnlModifierClient);
        pnlVisible.revalidate();
        pnlVisible.repaint();
    }

    public void afficherConsulterFacture(SearchResult result) {
        pnlVisible.removeAll();
        pnlConsulterFacture = new ConsulterFacture(this, result);
        setExtendedState(Frame.MAXIMIZED_BOTH);
        pnlVisible.add(pnlConsulterFacture);
        pnlVisible.revalidate();
        pnlVisible.repaint();
    }

    public void afficherModifierFacture(Facture facture) {
        pnlVisible.removeAll();
        pnlModifierFacture = new ModifierFacture(this, facture);
        setExtendedState(Frame.MAXIMIZED_BOTH);
        pnlVisible.add(pnlModifierFacture);
        pnlVisible.revalidate();
        pnlVisible.repaint();
    }

    public void afficherStatsClient(StatsClient stats) {
        pnlVisible.removeAll();
        pnlStatsClient = stats;
        pnlStatsClient.build();
        setExtendedState(Frame.MAXIMIZED_BOTH);
        pnlVisible.add(pnlStatsClient);
        pnlVisible.revalidate();
        pnlVisible.repaint();
    }

    public void afficherStatsCompletes(StatsCompletes stats) {
        pnlVisible.removeAll();
        pnlStatsCompletes = new StatsCompletes(this);
        setExtendedState(Frame.MAXIMIZED_BOTH);
        pnlVisible.add(pnlStatsCompletes);
        pnlVisible.revalidate();
        pnlVisible.repaint();
    }

    public void sauvegarderBD() throws IOException {
        new DB_Write(arClient, arEmploye, arEntree, arFactures, versionBD);
    }

    private void chargerBD() throws IOException, ClassNotFoundException {
        DB_Read r = new DB_Read(versionBD);
        arClient = r.getArClient();
        arEmploye = r.getArEmploye();
        arEntree = r.getArEntree();
        arFactures = r.getArFactures();
    }
}
